from app.utils.ros_utils import ROSUtils

class ArmService:
    def __init__(self, ros_utils):
        self.ros_utils = ros_utils
        
    
    def process(self):
        """启动机械臂处理过程"""
        print("启动机械臂处理过程")
        return self.ros_utils.call_rosservice('/a_control/process')
    
    def clear_error(self):
        """清除机械臂错误"""
        print("清除机械臂错误")
        return self.ros_utils.call_rostopic('/a_arm/error_clear', 'Bool', 'data: true')
    
    def go_home(self,state):
        """机械臂回原点"""
        print("机械臂回原点")
        return self.ros_utils.call_rosservice('/a_arm/return_to_origin',state)
    
    def read_cal(self,state):
        """读取机械臂标定值"""
        print("读取机械臂标定值")
        return self.ros_utils.call_rosservice('/a_arm/read_cal', state)
    def alert_cal(self,state):
        """修改机械臂标定值"""
        print("修改机械臂标定值")
        return self.ros_utils.call_rosservice('/a_arm/alert_cal', state)
    
    def set_shot(self, status):
        """设置机械臂绑扎开关"""
        if status == 1: # 开启机械臂绑扎
            return self.ros_utils.call_rostopic('/a_shot/isShot', 'Status', '{"code": true, "message": "", "data": ""}')
        elif status == 0: # 关闭机械臂绑扎
            return self.ros_utils.call_rostopic('/a_shot/isShot', 'Status', '{"code": false, "message": "", "data": ""}')
        
    def set_down(self, status):
        """设置机械臂下潜开关"""
        if status == 1: # 开启机械臂下潜
            return self.ros_utils.call_rostopic('/a_arm/isDown', 'Status', '{"code": true, "message": "", "data": ""}')
        elif status == 0: # 关闭机械臂下潜
            return self.ros_utils.call_rostopic('/a_arm/isDown', 'Status', '{"code": false, "message": "", "data": ""}')
        
    def emergency(self, status):
        """机械臂紧急处理"""
        if status == 1:  # 紧急停止
            print("机械臂急停")
            return self.ros_utils.call_rostopic('/web/webError', 'UInt16', 'data: 65279')
        elif status == 2:  # 暂停
            print("机械臂暂停")
            return self.ros_utils.call_rostopic('/web/webError', 'UInt16', 'data: 65278')
        else:  # 恢复
            print("机械臂恢复")
            return self.ros_utils.call_rostopic('/web/webError', 'UInt16', 'data: 0')